package com.calms.study.config;

import com.alibaba.druid.support.jakarta.StatViewServlet;
import com.alibaba.druid.support.jakarta.WebStatFilter;
import com.baomidou.mybatisplus.annotation.FieldStrategy;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.autoconfigure.MybatisPlusPropertiesCustomizer;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.BlockAttackInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.OptimisticLockerInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import lombok.extern.slf4j.Slf4j;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
import org.springframework.core.env.Environment;

/**
 * @author wangjunming
 * @since 2024-12-10 12:31
 */
@Slf4j
@Configuration
@MapperScan(basePackages = {"com.calms.study.infra.mapper","com.calms.study.config.dynamic_datasource"})
public class MybatisConfig {

  @Bean
  public MybatisPlusPropertiesCustomizer plusPropertiesCustomizer(Environment environment) {
    return plusProperties -> {
      plusProperties.getGlobalConfig().setBanner(false);
      // 此处增加配置将所有字段都进行更新, 如果为空, 则更新为空.
      plusProperties.getGlobalConfig().getDbConfig().setUpdateStrategy(FieldStrategy.ALWAYS);
      plusProperties.getConfiguration().setMapUnderscoreToCamelCase(true);
      if (Objects.isNull(environment.getProperty("mybatis-plus.global-config.db-config.id-type"))) {
        plusProperties.getGlobalConfig().getDbConfig().setIdType(IdType.AUTO);
      }
    };
  }

  @Bean
  @Order(100)
  public MybatisPlusInterceptor mybatisPlusInterceptor() {
    MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
    // 添加乐观锁控制
    interceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());
    // 自动分页插件
    interceptor.addInnerInterceptor(new PaginationInnerInterceptor());
    // 防止全表更新
    interceptor.addInnerInterceptor(new BlockAttackInnerInterceptor());
    return interceptor;
  }

  /** http://localhost:16099/druid */
  @Bean
  public ServletRegistrationBean<StatViewServlet> druidServlet() {
    log.info("init Druid Servlet Configuration ");
    ServletRegistrationBean<StatViewServlet> servletRegistrationBean =
        new ServletRegistrationBean();
    servletRegistrationBean.setServlet(new StatViewServlet());
    servletRegistrationBean.addUrlMappings("/druid/*");
    Map<String, String> initParameters = new HashMap<String, String>();
    initParameters.put("loginUsername", "admin"); // 用户名
    initParameters.put("loginPassword", "admin"); // 密码
    initParameters.put("resetEnable", "false"); // 禁用HTML页面上的“Reset All”功能
    initParameters.put("allow", ""); // IP白名单 (没有配置或者为空，则允许所有访问)
    // initParameters.put("deny", "192.168.20.38");// IP黑名单
    // (存在共同时，deny优先于allow)
    servletRegistrationBean.setInitParameters(initParameters);
    return servletRegistrationBean;
  }

  @Bean
  public FilterRegistrationBean<WebStatFilter> filterRegistrationBean() {
    FilterRegistrationBean<WebStatFilter> filterRegistrationBean = new FilterRegistrationBean();
    filterRegistrationBean.setFilter(new WebStatFilter());
    // 添加过滤规则
    filterRegistrationBean.addUrlPatterns("/*");
    // 添加不需要忽略的格式信息.
    filterRegistrationBean.addInitParameter(
        "exclusions", "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*");
    return filterRegistrationBean;
  }
}
